package com.apes.enquiry.brand.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.scm.account.tax.model.Tax;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;

/**
 * 功能：商品分类
 *
 * @author HuJianlin
 * @create 2018-03-15 16:00
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = "sp_mas_classify")
public class SpClassify extends Model {

    @Id
    @Column(name = "id", length = 16)
    @Comment("商品分类编码")
    private String id;  //商品分类编码

    @Column(name = "name", nullable = false, length = 120)
    @Comment("商品分类名称")
    private String name; //商品分类名称

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parents_id", referencedColumnName = "id")
    @Comment("上级分类编码")
    private SpClassify parents;  //上级分类编码

    @Column(name = "levels", nullable = false)
    @Comment("级别")
    private int levels; //级别

    @Column(name = "is_last_stage")
    @Comment("末级标记")
    private boolean lastStage;  //末级标记

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tax_id", referencedColumnName = "id")
    @Comment("销项税率")
    private Tax tax; //销项税率

    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("有效标记")
    private boolean valid; //有效

    @Transient
    private String path;

    public String getPath() {
        String path = null;
        if (parents != null) path = parents.getPath();
        return path != null ? path + "/" + id : id;
    }
}
